import 'package:flutter/material.dart';
import 'package:hkzf/widgets/common_image.dart';

class SearchBar extends StatefulWidget {
  final bool showLoaction; //展示位置按钮
  final Function goBackCallback; //回退按钮函数
  final String inputValue; // 搜索框输入值
  final String defaultInputValue; // 搜索框默认值
  final Function onCancel; //取消按钮
  final bool showMap; //展示地图按钮
  final Function onSearch; //用户点击搜索框触发
  final ValueChanged<String> onSearchSubmit;
  const SearchBar(
      {Key key,
      this.showLoaction,
      this.goBackCallback,
      this.inputValue,
      this.defaultInputValue = '请输入搜索词',
      this.onCancel,
      this.showMap = false,
      this.onSearch,
      this.onSearchSubmit})
      : super(key: key);

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  String _searchWord = '';
  TextEditingController _controller;
  FocusNode _focusNode;
  void _onClean() {
    setState(() {
      _searchWord = '';
      _controller.clear();
    });
  }

  @override
  void initState() {
    super.initState();
    _focusNode = new FocusNode();
    _controller = new TextEditingController(text: widget.inputValue);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        if (widget.showLoaction != null)
          Padding(
            padding: const EdgeInsets.only(right: 10.0),
            child: GestureDetector(
                child: Row(
              children: [
                Icon(
                  Icons.room,
                  color: Colors.grey,
                  size: 16,
                ),
                Text(
                  '北京',
                  style: TextStyle(color: Colors.black, fontSize: 14),
                )
              ],
            )),
          ),
        if (widget.goBackCallback != null)
          Padding(
            padding: const EdgeInsets.only(right: 10.0),
            child: GestureDetector(
              onTap: widget.goBackCallback,
              child: Icon(Icons.chevron_left, color: Colors.black87),
            ),
          ),
        Expanded(
          child: Container(
            height: 34,
            decoration: BoxDecoration(
                color: Colors.grey[200],
                borderRadius: BorderRadius.circular(17)),
            margin: const EdgeInsets.only(right: 10.0),
            child: TextField(
              focusNode: _focusNode,
              controller: _controller,
              onTap: () {
                if (widget.onSearchSubmit == null) {
                  _focusNode.unfocus();
                }
                widget.onSearch();
              },
              onSubmitted: widget.onSearchSubmit,
              style: TextStyle(fontSize: 14),
              onChanged: (String value) {
                setState(() {
                  _searchWord = value;
                });
              },
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                hintText: '请输入搜索词',
                contentPadding: EdgeInsets.only(top: 1, left: -10),
                hintStyle: TextStyle(color: Colors.grey, fontSize: 14),
                border: InputBorder.none,
                icon: Padding(
                  padding: const EdgeInsets.only(left: 10.0, top: 2),
                  child: Icon(
                    Icons.search,
                    size: 18,
                    color: Colors.grey,
                  ),
                ),
                suffixIcon: GestureDetector(
                  onTap: _onClean,
                  child: Icon(
                    Icons.clear,
                    size: 18,
                    color: _searchWord == '' ? Colors.grey[200] : Colors.grey,
                  ),
                ),
              ),
            ),
          ),
        ),
        if (widget.onCancel != null)
          Padding(
            padding: const EdgeInsets.only(right: 10.0),
            child: GestureDetector(
              child: Text(
                '取消',
                style: TextStyle(color: Colors.black, fontSize: 14),
              ),
              onTap: widget.onCancel,
            ),
          ),
        if (widget.showMap)
          CommonImage('static/icons/widget_search_bar_map.png')
      ],
    );
  }
}
